package com.edu.leetcode.dataStruct;


/**
 * @作者 five-five
 * @创建时间 2021/6/8
 */
public class CodePointConversion {
    public static void main(String[] args) {
        CodePointConversion codePointConversion = new CodePointConversion();
        String x = codePointConversion.convertText("(2502) A 2502 2494 2502");
        System.out.println(x);
    }

    /**
     * @param str e.g. (2502) A 2502 2494 2502
     * @return 转换后的简体
     */
    public String convertText(String str) {
        StringBuilder stringBuilder = new StringBuilder();
        String[] s = str.split(" ");
        for (String s1 : s) {
            int isop = 0;
            //标准码点数
//            if (s1.length() == 4 && s1.matches("^[0-9]{4}$")) {
            if (s1.length() == 4 && s1.matches("^([-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?))$")) {
                isop++;
                //判断当前码点是否有效
                int codePoint = Integer.parseInt(s1);
                if (Character.isValidCodePoint(codePoint)) {
                    //拼接
                    stringBuilder.append(String.valueOf(Character.toChars(codePoint)));
                }
            }
            //带括号的标准码点数
            if (s1.length() > 4) {
                char first = s1.charAt(0);
                char last = s1.charAt(s1.length() - 1);
                if (first == '(') {
                    stringBuilder.append("(");
                }
                s1 = s1.substring(1, s1.length());
                if (last == ')') {
                    s1 = s1.substring(0, s1.length() - 1);
                }
                if (s1.length() == 4 && s1.matches("^[0-9]{4}$")) {
//                if (s1.length() == 4 && s1.matches("^([-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?))$")) {
                    //判断当前码点是否有效
                    int codePoint = Integer.parseInt(s1);
                    if (Character.isValidCodePoint(codePoint)) {
                        //拼接
                        stringBuilder.append(String.valueOf(Character.toChars(codePoint)));
                    }
                }
                if (last == ')') {
                    stringBuilder.append(")");
                }
                isop++;
            }
            if (isop == 0) {
                stringBuilder.append(s1);
            }

        }
        return stringBuilder.toString();
    }

}
